package com.gopay.bis.nucc.ebank.impl;

import com.gopay.bis.nucc.common.util.NuccCertHelper;
import com.gopay.bis.nucc.common.util.NuccHttpClientUtil;
import com.gopay.common.constants.bank.NuccConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.nucc.common.base.NuccBaseMessage;
import com.gopay.common.domain.bank.nucc.ebank.pay.end.NuccEbankEndRes;
import com.gopay.common.domain.bank.nucc.ebank.pay.res.NuccEbankRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.businesses.NuccEbankService;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 网关支付
 * Created by zhoutong on 2018/1/15.
 */
public class NuccEbankServiceImpl implements NuccEbankService {

    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(NuccEbankServiceImpl.class);

    /**
     * 安全工具类
     */
    private NuccCertHelper nuccCertHelper;

    /**
     * 通讯工具类
     */
    private NuccHttpClientUtil nuccHttpClientUtil;

    @Override
    public Object payment(BaseMessage baseMsg) throws GopayException {
        NuccBaseMessage message = (NuccBaseMessage) baseMsg;
        NuccEbankRes res;
        try{
            message = nuccCertHelper.sign(message);
            Map<String,String> map = new HashMap<>();
            map.put("MsgTp", message.getMsgHeader().getMsgTp());
            map.put("OriIssrId", message.getMsgHeader().getIssrId());
            String response = nuccHttpClientUtil.send(message.getPayUrl(), message.getPlainMessage()+message.getSignMessage(), map);
            res = (NuccEbankRes) nuccCertHelper.verify(response,  NuccEbankRes.class);
            if(res != null){
                res.setPlainMessage(response);
            } else {
                logger.error("网联网关支付申请异常，返回跳转报文为空");
                throw new GopayException(ProcCodeConstants.PROC_CODE_100E6128);
            }
        }catch (GopayException e) {
            logger.error("网联网关支付申请异常" , e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6128,e);
        } catch (Exception ex) {
            logger.error("网联网关支付申请异常" , ex);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6128,ex);
        }
        return res;
    }

    @Override
    public Object verify(BaseMessage baseMsg) throws GopayException {
        NuccEbankEndRes res;
        try{
            res = (NuccEbankEndRes) nuccCertHelper.verify(baseMsg.getSignMessage(),  NuccEbankEndRes.class);
            if(res == null){
                logger.error("网联网关支付终态通知异常");
                throw new GopayException(ProcCodeConstants.PROC_CODE_100E6128);
            }
        }catch (GopayException e) {
            logger.error("网联网关支付终态通知异常" , e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6128,e);
        } catch (Exception ex) {
            logger.error("网联网关支付终态通知异常" , ex);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6128,ex);
        }
        return res;
    }

    @Override
    public Object refund(BaseMessage baseMsg) throws GopayException {
        return null;
    }

    public NuccCertHelper getNuccCertHelper() {
        return nuccCertHelper;
    }

    public void setNuccCertHelper(NuccCertHelper nuccCertHelper) {
        this.nuccCertHelper = nuccCertHelper;
    }

    public NuccHttpClientUtil getNuccHttpClientUtil() {
        return nuccHttpClientUtil;
    }

    public void setNuccHttpClientUtil(NuccHttpClientUtil nuccHttpClientUtil) {
        this.nuccHttpClientUtil = nuccHttpClientUtil;
    }
}
